//
//  main.cpp
//  min-cut
//
//  Created by Emanuele Vespa on 5/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include "ParallelMinCut.h"


void help();



int main (int argc, const char * argv[])
{
    
    ofstream file;
    stringstream ss, str, filepath;
   
    
    list<int>::iterator it;
    MultiGraph g;
    
    srand(time(NULL));
    
    tbb::tick_count start, end;
    
    char hostname[40];
    gethostname(hostname, 40);
    float time;


   
    if(argc >= 2){
        
        if( strcmp(argv[1], "--parallel") == 0 ){   // parallel implementation
            
            if(argc == 4) {
                
                filepath << argv[3];
                
                
                g.readGraph( filepath.str().c_str() );
                
                if(!g.DFS()) return 0;

                
                
                 ss << "logs/" << hostname << "-performance-" << argv[2] << "-" << g.vertexNum << "-log.txt"; // performance logs
                
                start = tbb::tick_count::now();
                g.ParallelKarger(atoi(argv[2]));
                end = tbb::tick_count::now();
                
                time = (end - start).seconds();
                
                file.open ( ss.str().c_str() , ios::app);
                
                file << time << "\t"  << g.iteration_min << "\t" << g.cut_weight << "\t" << argv[2] << endl; // performance logs
                file.flush();
                file.close();
                
                            
            }
            
                        
            
        }
        
        
        if( strcmp(argv[1], "--sequential") == 0 ){    // sequential implementation
            
           
            
            filepath << argv[2];
            
            
            g.readGraph( filepath.str().c_str() );
            
             int num_runs = g.vertexNum*g.vertexNum;
            
            start = tbb::tick_count::now();
            g.KargerMinCut(num_runs);
            
            end = tbb::tick_count::now();
            
            time = (end - start).seconds();
            
            ss << "logs/" << hostname << "-performance-sequential" << "-log.txt";   // performance logs
            
            file.open ( ss.str().c_str() , ios::app);
            
            file << time << "\t"  << g.iteration_min << "\t" << g.cut_weight << "\t" << argv[2] << endl;    // performance logs
            
            file.close();
            
            file.open ( str.str().c_str(), ios::app);
            file << time << endl;
            file.close();

            
        }
        
    }
    else help();
    
    
    
    return 1;
}

void help(){
    
    cout << endl;
    cout << "Usage: " << endl;
    cout << "type 'run-mincut --sequential' to run the sequential version" << endl;
    cout << "type 'run-mincut --parallel <num_thread>' to run in parallel and specify the number of threads" << endl;
    
    
    
}

